import { getCookie } from "./cookie";
import { IOS_FORBIDDEN_TYPE, ASSOCIATION } from "./constants";

// 小鹅SDK已经在公共模板引入
const sdkInfo = window?.wx || {};

// 微信浏览器UA
export const UA_WXCHAT = 'micromessenger';

// 微信小程序UA
export const UA_MINI_PROGRAM = 'miniprogram';

// 用户UA
export const UA_USER = window.navigator.userAgent.toLowerCase();

// 小鹅店小程序
export const isXiaoShop = sdkInfo.CLIENT_ENV === "shop_miniProgram";

// 是否为IOS系统
export const isIos = sdkInfo.CLIENT_SYSTEM === "IOS";

// 是否为Mac系统
export const isMac = sdkInfo.CLIENT_SYSTEM === "mac";

// 是否为店铺小程序
export const isMP = sdkInfo.CLIENT_ENV === "wx_miniProgram" || isXiaoShop;

// ios微信小程序
export const isMiniprogramInIos = UA_USER.includes(UA_MINI_PROGRAM) && isIos;

// 纯店铺小程序
export const isMpShop = sdkInfo.CLIENT_ENV === "wx_miniProgram";

// 是否为微信环境
// 请注意，iSMp 为true, 并不代表isWechat为true
export const isWechat = sdkInfo.CLIENT_ENV === "wechat";
// isWechat 在jssdk中会以url的app_env_type进行判断，再对CLIENT_ENV重新赋值，所以CLIENT_ENV不能正确代表微信浏览器环境
// 所以需要判断UA，若UA有微信浏览器标识且没有小程序标识，那说明是在微信浏览器中
// isWechatBrowser 是否微信浏览器环境
export const isWechatBrowser = UA_USER.includes(UA_WXCHAT) && !UA_USER.includes(UA_MINI_PROGRAM);

// 是否为鹅学习app
export const isELearnApp = sdkInfo.CLIENT_ENV === "xiaoehelperapp";

// 是否为鹅学习小程序
export const isELearnMP = sdkInfo.CLIENT_ENV === "wx_miniProgram_xiaoeLearn";

// 是否为独立圈子小程序 店铺级别
export const isOnlyCMN =
  sdkInfo.CLIENT_ENV === "wx_miniProgram_merchant_community";

// 是否为鹅圈子小程序 中心化
export const isMutilCMN = sdkInfo.CLIENT_ENV === "wx_miniProgram_ecommunity";

// 是否为SDK
export const isSDK = sdkInfo.CLIENT_ENV === "xiaoeapp";

// 是否为企业直播
export const isQlive = sdkInfo.SHOP_TYPE_ENV === "qlive_shop";

// 是否为品牌App
export const isBrandApp = sdkInfo.CLIENT_ENV === "xiaouniapp";

export const isWuXiaoBoApp = isBrandApp && navigator.userAgent?.toLocaleLowerCase()?.includes('wxbapp')

//是否为圈子App
export const isEcommunityApp = sdkInfo.CLIENT_ENV === "ecommunityapp"
// export const isEcommunityApp = true

// 是否为圈子小程序
export const isCMNMp = isOnlyCMN || isMutilCMN;

// 是否为 ios 中的圈子小程序
export const CMNMpInIos = isCMNMp && isIos;

// 是否为圈子商品类型处于圈子小程序
export const isCMNInMp = function (type) {
  return (isOnlyCMN || isMutilCMN) && type === ASSOCIATION;
};

export function deviceScrm() {
  return sdkInfo.SHOP_TYPE_ENV === "scrm_assistant";
}

/**
 *
 * @desc env 参数说明
 * qlive_shop 企业直播
 */
export function getClientEnv(env) {
  return sdkInfo.SHOP_TYPE_ENV === env;
}

//鹅学习小程序环境判断
export function deviceExuexi() {
  return window.wx.CLIENT_ENV === "wx_miniProgram_xiaoeLearn";
}

// 视频号分销小程序场景(暂时用作小鹅店小程序判断)
// 小鹅店小程序
export const isVideoDistribution = isXiaoShop; // window.wx && window.wx.CLIENT_CHANNEL === "wx_video"

// 是否为快手小程序
export const isKSMiniProgram =
  sdkInfo.CLIENT_ENV === "ks_miniProgram" ||
  sessionStorage.getItem("app_env_type") === "ks_miniProgram";

// ios环境下的鹅学习app
export const ELearnAppInIos = isELearnApp && isIos;

// ios环境下的鹅学习app 受b端开关影响 只处理虚拟商品类型 受app中标识影响（低于特定版本不会有该cookie）
export function IosELearnAppSwitch(vm) {
  const {
    supportIosVirtual: support,
    baseInfo: { spu_type },
  } = vm;
  const isIosForbidden = IOS_FORBIDDEN_TYPE.includes(spu_type);
  return (
    ELearnAppInIos &&
    !support &&
    isIosForbidden &&
    Boolean(getCookie("enable_ios_iap"))
  );
}

// ios环境小程序
export const IosMp = isMP && isIos;

// ios环境小程序, 包含小鹅通小程序
export const IosMpMore = isIos && (isMP || isELearnMP);

// 虚拟币支付环境
export const enableAppPayment =
  isIos &&
  isSDK &&
  getCookie("enable_app_payment") &&
  Number(getCookie("enable_app_payment")) === 1;

// 品牌app虚拟币ios端
export const brandios = isBrandApp && isIos;

// 品牌app虚拟币ios端
export const brandNotIos = isBrandApp && isIos;

// 是否是pc端
export const isPc = window.wx && window.wx.CLIENT_TYPE === "PC";

// 小程序SDK
export const isMiniProgramSdk = sdkInfo.CLIENT_ENV === 'wx_program_wechat_sdk';

export function isAndroid() {
  let u = navigator.userAgent;
  return u.indexOf("Android") > -1 || u.indexOf("Linux") > -1; //安卓
}

export function isMiniEnv() {
  return (
    isMP ||
    isELearnMP ||
    isKSMiniProgram ||
    isVideoDistribution ||
    isOnlyCMN ||
    isMutilCMN ||
    isMiniProgramSdk
  );
}

export function isApp() {
  return isELearnApp || isSDK || isBrandApp || isEcommunityApp;
}

export function isH5() {
  return !isPc && !isMiniEnv();
}

// 微信小程序
export const inMiniprogram = UA_USER.includes(UA_MINI_PROGRAM);

// 直播iframe场景
export const isAliveIframe = window.parent?.channel === "isLiveCommerce";